package com.dd.web.base.service.impl;

import com.dd.common.entity.ResultCode;
import com.dd.common.exception.RequestParamException;
import com.dd.web.base.common.constant.StatusEnum;
import com.dd.web.base.constant.DictConstant;
import com.dd.web.base.dao.UmsDictDataDao;
import com.dd.web.base.entity.common.CommonPage;
import com.dd.web.base.entity.data.UmsDictData;
import com.dd.web.base.entity.data.UmsDictType;
import com.dd.web.base.entity.data.UmsMenu;
import com.dd.web.base.entity.view.DictDataVo;
import com.dd.web.base.entity.view.DictListParam;
import com.dd.web.base.service.UmsDictDataService;
import com.dd.web.base.service.UmsDictTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jdk.net.SocketFlow;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 数据字典
 */
@Service
public class UmsDictDataServiceImpl implements UmsDictDataService {


    @Autowired
    private UmsDictDataDao dictDataDao;

    @Autowired
    private UmsDictTypeService dictTypeService;

    @Override
    public CommonPage<DictDataVo> selectDictDataList(DictListParam param) {
        Page<List<UmsDictData>> pageHelp = PageHelper.startPage(param.getPageNum(),param.getPageSize());
        List<DictDataVo> data = this.dictDataDao.selectDictDataList(param);
        CommonPage resultPage = CommonPage.restPage(data);
        return resultPage;
    }

    @Cacheable(value = DictConstant.REDIS_CACHE_DATA,key = "#p0",condition = "#result != null")
    @Override
    public List<UmsDictData> selectDictDataByType(String dictType) {
        return dictDataDao.selectDictDataByType(dictType);
    }


    /**
     * 根据字典数据ID查询信息
     *
     * @param id 字典数据ID
     * @return 字典数据
     */
    @Override
    public UmsDictData selectDictDataById(Long id)
    {
        return dictDataDao.selectDictDataById(id);
    }

    /**
     * 通过字典ID删除字典数据信息
     *
     * @param id 字典数据ID
     * @return 结果
     */
    @CacheEvict(value = DictConstant.REDIS_CACHE_DATA,key="#p0")
    @Override
    public int deleteDictDataById(Long id)
    {
        return dictDataDao.deleteDictDataById(id);
    }


    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @CacheEvict(value = DictConstant.REDIS_CACHE_DATA,key="#p0.id")
    @Override
    public int insertDictData(DictDataVo dictData)
    {
        //设置操作人
//        dictData.setCreateBy("");
        //验证字典类型是否存在
        UmsDictType type = this.dictTypeService.selectDictTypeByType(dictData.getDictType());
        //字典类型不存在时，保存字典类型，并验证字典类型名称不能为空
        if (type == null){
            UmsDictType dictTypeVo = new UmsDictType();
            if (StringUtils.isBlank(dictData.getDictName())){
                //字典名称为空，抛出参数错误异常，并设定错误字段
                throw new RequestParamException(ResultCode.VALIDATE_FAILED_ISNULL,"dictName");
            }
            dictTypeVo.setDictName(dictData.getDictName());
            dictTypeVo.setDictType(dictData.getDictType());
            this.dictTypeService.insertDictType(dictTypeVo);
        }
        //保存字典数据
        return dictDataDao.insertDictData(dictData);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @CacheEvict(value = DictConstant.REDIS_CACHE_DATA,key="#p0.dictType")
    @Override
    public int updateDictData(DictDataVo dictData)
    {
        return dictDataDao.updateDictData(dictData);
    }

    @Override
    public int doStopDictData(Long[] ids) {
        return this.dictDataDao.updateDictDataStatus(ids, StatusEnum.STOP.getCode()+"");
    }

    @Override
    public int doStratDictData(Long[] ids) {
        return this.dictDataDao.updateDictDataStatus(ids, StatusEnum.ACTIVE.getCode()+"");
    }
}
